<template>
  <div class="app-container">
    <u-page-config
      :filter-array="filterArray"
      v-model="filter"
      :actionBtn="actionBtn"
      label-width="100px"
      :getList="getList"
      :tableData="tableData"
      :tableColumn="tableColumn"
      :total="total"
      :loading="loading"
      showSelection
      @handleSelectionChange="handleSelectionChange"
      @addRow="addRow"
      @removeRow="removeRow"
      @editRow="editRow"
      @rowDetail="rowDetail"
      @export="handleExport"
    >

      <template #branchCode="scope">
          <treeselect
            style="width:240px"
            v-model="filter.data.branchCode"
            :options="branchDataOptions"
            :normalizer="branchNormalizer"
            :show-count="true"
            placeholder="选择网点"
            noOptionsText="暂无选项"
            noResultsText="没有匹配的搜索结果"
          />
        </template>
    </u-page-config>

    <el-dialog
      :title="title"
      :visible.sync="visible"
      width="1200px"
      :close-on-click-modal="false"
    >
      <u-form
        ref="uFormRef"
        label-width='134px'
        v-model="form"
        :defaultForm="defaultForm"
        :formArray="formArray"
        :show-btn="false"
        @submit="onsubmit"
        @close="visible = false"
        :mode="mode"
      >
        <template #branchCode="scope">
          <!-- 1 -->
          <treeselect
            style="width:100%"
            v-model="form.branchCode"
            :options="branchDataOptions"
            :normalizer="branchNormalizer"
            :show-count="true"
            placeholder="选择网点"
            noOptionsText="暂无选项"
            noResultsText="没有匹配的搜索结果"
          />
        </template>

        <template #image="scope">
          <u-file-upload
            :value="form.pic"
            :limit="3"
            :fileSize="10"
            @input="imageUploadChange"
          />
        </template>
      </u-form>
    </el-dialog>
  </div>
</template>

<script>
import UPageConfig from '@/components/uPageConfig/index'
import UForm from '@/components/uForm'
import table from "@/mixin/uTable";
// 2
import Treeselect from "@riophae/vue-treeselect";

import {
  getWorkOrderList,
  addWorkOrder,
  updateWorkOrder,
  WorkOrderDetail,
  deleteWorkOrder
} from '@/api/operationAndMonitoring/workOrder'
import {
  listFwzx,
  getFwzx,
  delFwzx,
  addFwzx,
  updateFwzx,
} from "@/api/info/fwzx";
import {listManager} from "@/api/device/placeManager";
// import FillFormDialog from "@/views/device/adapterManagement/components/fillFormDialog";

export default {
  name: "UpdatePlan",
  mixins: [table],
  // 3
  components: {UPageConfig, UForm, Treeselect},
  data() {
    return {
      filter:{
        pageSize: 10,
        pageNum: 1,
        data:{
          branchCode: undefined
        }
      },
      startOptions: [
        {label: '1星', value: 1},
        {label: '2星', value: 2},
        {label: '3星', value: 3},
        {label: '4星', value: 4},
        {label: '5星', value: 5},
      ],
      filterArray: [
        {label: "姓名", key: "name", type: "input"},
        {label: "部门名称", key: "deptname", type: "input"},
        {label: "网点", key: "branchCode", type: "slot",slot: 'branchCode'},
        {label: "评分", key: "score", type: "select", options: []},
        // {label: "所属运维人员", key: "operationUserCode", type: "select", options: []},
        // {label: "创建时间", key: "daterange", type: "daterange", noRequestParams: true},
      ],
      tableColumn: [
        {label: "ID", prop: "id"},
        {label: "姓名", prop: "name"},
        {label: "部门名称", prop: "deptname"},
        {label: "评分", prop: "score"},
        {label: "照片", prop: "pic", type: 'image'},
        {label: "网点", prop: "branchName"},
        {label: "简要描述", prop: "description",showOverflowTooltip: true},
        {
          label: "操作", type: "btn", attr: {width: 180}, btnArr: [
            {
              icon: "el-icon-edit-outline",
              text: "修改",
              emit: "editRow",
              permissions: 'operationAndMonitoring:workOrder:edit',
              disabledRule: (row) => row.orderStatus === 1
            },
            {
              icon: "el-icon-delete",
              text: "删除",
              emit: "removeRow",
              permissions: 'operationAndMonitoring:workOrder:remove'
            },
          ]
        }
      ],
      actionBtn: [
        {
          type: 'primary',
          text: '创建',
          icon: 'el-icon-plus',
          emit: 'addRow',
          permissions: 'info:fwzx:add'
        },
        {
          type: 'success',
          text: '修改',
          icon: 'el-icon-edit',
          emit: 'editRow',
          permissions: 'info:fwzx:edit',
          disabledRule: () => this.ids.length !== 1
        },
        {
          type: 'danger',
          text: '删除',
          icon: 'el-icon-delete',
          emit: 'removeRow',
          permissions: 'info:fwzx:remove',
          disabledRule: () => this.ids.length === 0
        },
        {
          type: 'warning',
          text: '导出',
          icon: 'el-icon-download',
          emit: 'export',
          permissions: 'info:fwzx:export'
        },
      ],
      // 表单默认必填参数 重置表单时需要用到
      defaultForm: {
        id: null,
        name: null,
        deptname: null,
        score: null,
        description: null,
        pic: null,
        branchCode: null,
        branchName: null,
        deptId: null,
        createUserId: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      },
      formArray: [
        {
          label: "姓名: ", key: "name", col: 24, type: "input",
          rule: {required: true, message: '姓名不能为空！', trigger: 'blur'},
        },
        {
          label: "部门名称: ", key: "deptname", col: 24, type: "input",
          rule: {required: true, message: '部门名称不能为空！', trigger: 'blur'},
        },
        {
          label: "网点: ", key: "branchCode", col: 24, type: "slot", slot: 'branchCode',
          rule: {required: true, message: '网点不能为空！', trigger: 'blur'},
        },
        {
          label: "评分: ", key: "score", col: 24, type: "select", options: [],
          rule: {required: true, message: '评分不能为空！', trigger: 'change'},
        },
        {label: "照片：", key: "pic", col: 24, slot: "image"},
        {
          label: "简要描述: ", key: "description", col: 24, type: "textarea"
        },
      ],
      // 查看详情表单配置
      detailArray: [
        {
          label: "设备ID：", key: "deviceId", col: 24, type: "text",
        },
        {label: "设备部署名称：", key: "deploymentName", col: 24, type: "text", placeholder: ' '},
        {
          label: "运维人员：", key: "operationUserCode", col: 24, type: "select", options: [],
        },
        {label: "问题描述：", key: "message", col: 24, type: "text", attr: {resize: 'none'}},
        {label: "照片：", key: "image", col: 24, type: "img"},
        {
          label: "报障人联系电话：", key: "reportPhone", col: 24, type: "text",
        },
        {
          label: "报障人姓名：", key: "reportName", col: 24, type: "text",
        },
      ],
    };
  },
  computed:{
    // 4
    // 网点树形数据
    branchDataOptions() {
      return this.$store.state.device.branchDataOptions || []
    },
  },
  methods: {
    // 5
    /** 转换分组树形图数据结构 */
    branchNormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.branchCode,
        label: node.branchName,
        children: node.children || undefined,
      };
    },
    // 表单提交接口相关配置
    formSubmitFunConfig() {
      return {
        addFunc: addFwzx,
        modifyFunc: updateFwzx,
        pk: 'id',
        options: {
          ...this.form
        },
        cbFunc: () => {
          this.visible = false
        }
      }
    },
    // 删除
    removeRow(row) {
      const id = row ? row.id : this.ids;
      this.removeTip(async () => {
        await delFwzx(id)
      }, row && row.id ? '该项数据' : '已勾选项数据')
    },
    // 添加
    addRow() {
      this.mode = 'form';
      this.title = "添加服务之星";
      this.visible = true;
    },
    // 修改
    async editRow(row) {
      this.mode = 'form'
      this.title = "修改服务之星";
      if (row) { //点击的表格内修改按钮
        this.form = JSON.parse(JSON.stringify(row))
      } else { //点击表格上方修改按钮
        const selectedItem = this.tableData.find(item => this.ids[0] === item.id) || {}
        this.form = JSON.parse(JSON.stringify(selectedItem))
      }
      this.visible = true
      await this.$nextTick()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
    },
    // 添加文件上传成功、删除图片时都会被调用
    imageUploadChange(imgUrl) {
      this.form.pic = imgUrl
    },
    /** 查询工单列表 */
    getList() {
      this.getInit(listFwzx, {})
    },
    /** 详细按钮操作 */
    rowDetail(row) {
      this.mode = 'text'
      // 直接拿到这一行的数据赋值
      this.form = row;
      this.visible = true
      this.title = '详细信息'
      // 如果当前行数据不完整 调接口拿详情数据
      // this.getDetail('详细信息', WorkOrderDetail, row.id)
    },
    /** 返回设备部署页面 */
    goBack() {
      this.$router.go(-1);
    },
    /** 导出按钮操作 */
    handleExport() {
      const currentDate = this.parseTime(new Date());
      const ids = this.ids;
      const params = this.getListRequestParams();
      console.log("列表查看::",params)
      this.download(
        "/info/fwzx/export",
        params,
        `服务之星_${currentDate}.xlsx`,
      );
    },
  },
  created() {
    try {
      this.filterArray.find(item => item.key === 'score').options = this.startOptions;
      // 绑定选择列表
      this.formArray.find(item => item.key === 'score').options = this.startOptions;
    } catch (e) {
      console.log("找不到对应表单项", e)
    }
  }
}
</script>

<style lang="scss" scoped>
.form-item-wrap {
  display: flex;
}
</style>
